package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.product.domain.ProductClass;
import com.un.ebs.product.dto.ProductClassAddDto;
import com.un.ebs.product.dto.ProductClassUpdateDto;
import com.un.ebs.product.service.ProductClassService;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/productClass")
@Api(value = "product-class", description = "品类")
public class ProductClassController {

    @Autowired
    private ProductClassService service;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody ProductClassAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("addBatch")
    @ApiOperation(value = "addBatch", notes = "批量添加")
    public WebResult addBatch(@RequestBody List<ProductClassAddDto> req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody ProductClassUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("changeParent")
    @ApiOperation(value = "changeParent", notes = "修改 ProductClass 的 parentId 属性")
    public WebResult changeParent(@RequestParam String id, @RequestParam String parentId) {
        service.changeParent(id, parentId);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 ProductClass 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @PostMapping("changeNode")
    @ApiOperation(value = "changeNode", notes = "批量修改节点中的父级对象和排序")
    public WebResult changeNode(@RequestBody List<ObjectParentAndIndexChangeDto> nodes) {
        service.changeNode(nodes);
        return WebUtils.success();
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ProductClass")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 ProductClass")
    public WebResult<ProductClass> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "查询当前租户所有品类")
    public WebResult<List<ProductClass>> list() {
        LambdaQueryWrapper<ProductClass> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByAsc(ProductClass::getSort);
        return WebUtils.success(service.list(wrapper));
    }

    @GetMapping("listRoot")
    @ApiOperation(value = "listRoot", notes = "查询当前租户所有品类的一级分类")
    public WebResult<List<ProductClass>> listRoot() {
        LambdaQueryWrapper<ProductClass> wrapper = Wrappers.lambdaQuery();
        wrapper.isNull(ProductClass::getParentId);
        wrapper.eq(ProductClass::getIsDelete, false);
        wrapper.orderByAsc(ProductClass::getSort);
        return WebUtils.success(service.list(wrapper));
    }

    @GetMapping("/child/{parentId}")
    @ApiOperation(value = "listChild", notes = "查询指定 parentId 属性的 ProductClass")
    public WebResult<List<ProductClass>> listChild(@PathVariable("parentId") String parentId) {
        return WebUtils.success(service.queryByParentId(parentId));
    }

    @GetMapping("/childPage/{parentId}/{pageSize}/{pageIndex}")
    @ApiOperation(value = "listPageChild", notes = "查询指定 parentId 属性的 ProductClass")
    public WebResult<IPage<ProductClass>> listPageChild(@PathVariable("parentId") String parentId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) {
        return WebUtils.success(service.queryPageByParentId(parentId, pageSize, pageIndex));
    }

}